//
//  alipayViewController.m
//  ZhenPin
//
//  Created by tmosport on 14-7-9.
//  Copyright (c) 2014年 zhenpin. All rights reserved.
//

#import "alipayViewController.h"
#import "MBProgressHUD.h"
@interface alipayViewController ()
{
    MBProgressHUD *hud;
}
@property (strong, nonatomic) IBOutlet UIWebView *alipayWebView;

@end

@implementation alipayViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"在线支付";
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]
                                            initWithTitle:@"返回"
                                            style:UIBarButtonItemStyleBordered
                                            target:self
                                            action:@selector(dismiss)];

    
//    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://m.zhenpin.com/index.php?c=APP_my&a=alipay&ordersn=%@&orderid=%@&token=%@&memberid=%@&needpay=%@",_ordersn,_orderId,_token,_memberId,_needpay]];
    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://m.zhenpin.com/index.php?c=APP_my&a=alipay&ordersn=%@&orderid=%@&token=%@&memberid=%@&needpay=%@",_ordersn,_orderId,_token,_memberId,_needpay]];
    [_alipayWebView loadRequest:[NSURLRequest requestWithURL:url]];
}

- (void)dismiss{
    
    [self dismissViewControllerAnimated:YES completion:^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"do" object:self];
    }];
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSURL *url = [request URL];//(@"url's scheme is :%@",[url scheme]);
    if ([[url scheme] isEqualToString:@"zpmall"]) {
        //(@"%@",[url query]);
        [self goHome];
    }
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"加载中...";
    hud.dimBackground = NO;
    [hud hide:YES afterDelay:3];
}

//-(void)webViewDidFinishLoad:(UIWebView *)webView
//{
//    hud.labelText = @"加载完成";
//    [hud hide:YES];
//}
//- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
//{
//    hud.labelText = @"加载失败";
//    [hud hide:YES afterDelay:1];
//}

-(void)goHome{

    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
